import axios from '@/utils/request'

import type { IPageResult } from './types/base'
import { IClaimType } from './types/claimTypes'
import { IRole, IRoleClaim } from './types/role'

// 获取角色列表
export function getRoles(
  currentPage = 1,
  take = 10,
  params: {
    filter: string
  },
) {
  return axios<IPageResult<IRole>>({
    url: '/api/identity/roles',
    method: 'get',
    params: {
      skipCount: take * (currentPage - 1),
      maxResultCount: take,
      ...params,
    },
  })
}

export function addRole(data: any) {
  return axios({
    url: '/api/identity/roles',
    method: 'post',
    data,
  })
}

export function getRole(id: string) {
  return axios<IRole>({
    url: `/api/identity/roles/${id}`,
    method: 'get',
  })
}

export function editRole(id: string, data: any) {
  return axios({
    url: `/api/identity/roles/${id}`,
    method: 'put',
    data,
  })
}

export function deleteRole(id: string) {
  return axios({
    url: `/api/identity/roles/${id}`,
    method: 'delete',
  })
}

// 获取所有声明
export function getAllRoleClaimTypes() {
  return axios<IClaimType[]>({
    url: '/api/identity/role/all-claim-types',
    method: 'get',
  })
}
// 获取当前用户的声明
export function getRoleClaims(roleId: string) {
  return axios<IRoleClaim[]>({
    url: `/api/identity/role/${roleId}/claims`,
    method: 'get',
  })
}

// 保存当前用户的声明
export function setRoleClaims(roleId: string, data: IRoleClaim[]) {
  return axios({
    url: `/api/identity/role/${roleId}/claims`,
    method: 'put',
    data,
  })
}
